package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.exception.LeaseException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.web.admin.mapper.LeaseAgreementMapper;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.agreement.AgreementQueryVo;
import com.atguigu.lease.web.admin.vo.agreement.AgreementVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author liubo
 * @description 针对表【lease_agreement(租约信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class LeaseAgreementServiceImpl extends ServiceImpl<LeaseAgreementMapper, LeaseAgreement>
        implements LeaseAgreementService {
    @Autowired
    private LeaseAgreementMapper leaseAgreementMapper;
//这里注入service 和 mapper 都可以
    @Autowired
    private ApartmentInfoService apartmentInfoService;
    @Autowired
    private RoomInfoService roomInfoService;
    @Autowired
    private PaymentTypeService paymentTypeService;
    @Autowired
    private LeaseTermService leaseTermService;

    @Override
    public void customPage(Page<AgreementVo> page, AgreementQueryVo queryVo) {
    //    page = leaseAgreementMapper.customQueryPage(page, queryVo);
        //使用 page = 可以生成Mapper层的 Page<AgreementVo> 不用自己写了
        //    Page<AgreementVo> customQueryPage(Page<AgreementVo> page, AgreementQueryVo queryVo);
        leaseAgreementMapper.customQueryPage(page, queryVo);
    }

    @Override
    public AgreementVo queryById(Long id) {
        //1.租约id查询租约对象
        LeaseAgreement leaseAgreement = getById(id);
        // 判断租约对象是否为空
        if (leaseAgreement == null){
            // 如果为空，抛出异常
            throw  new LeaseException(ResultCodeEnum.FAIL);
        }
        //2.租约提供的四个其他的id查询其他对象
        ApartmentInfo apartmentInfo = apartmentInfoService.getById(leaseAgreement.getApartmentId());
        RoomInfo roomInfo = roomInfoService.getById(leaseAgreement.getRoomId());
        PaymentType paymentType = paymentTypeService.getById(leaseAgreement.getPaymentTypeId());
        LeaseTerm leaseTerm = leaseTermService.getById(leaseAgreement.getLeaseTermId());
        //3.将对象组合成vo对象并返回
        AgreementVo agreementVo = new AgreementVo();
        agreementVo.setApartmentInfo(apartmentInfo);
        agreementVo.setRoomInfo(roomInfo);
        agreementVo.setPaymentType(paymentType);
        agreementVo.setLeaseTerm(leaseTerm);
        BeanUtils.copyProperties(leaseAgreement,agreementVo);
        return agreementVo;
    }
}




